package com.example.tests;

import com.example.framework.GroupData;
import com.example.utils.SortedListOf;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import static com.example.utils.GroupDataGenerator.loadGroupsFromXmlFile;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;

public class GroupCreationTest extends TestManager {

    @DataProvider
    public Iterator<Object[]> groupsFromFile() throws IOException {
        List<GroupData> groups = loadGroupsFromXmlFile(new File("groups.xml"));
        List<Object[]> list = convertGroupToDataProviderFormat(groups);

        return list.iterator();
    }

    @Test(dataProvider = "groupsFromFile")
    public void shouldCreateGroupWithValidData(GroupData group) {
        // save old state
        SortedListOf <GroupData> oldGroupList = new SortedListOf<GroupData>(manager.getHibernateHelper().listGroups());

        // actions
        manager.getGroupHelper().createNewGroup(group);

        // save new state
        SortedListOf<GroupData> newGroupList = manager.getGroupHelper().getGroups();

        // compare states
        assertThat(newGroupList, equalTo(oldGroupList.withAdded(group)));
    }
}
